package com.dream.testtubebaby.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.activity.PublishBbsActivity;
import com.dream.testtubebaby.activity.SearchTopicActivity;
import com.dream.testtubebaby.adapter.CommonTabAdapter;

public class BbsFragment extends Fragment implements OnClickListener {
	public static final int BBS_TYPE_ALL = 1;
	public static final int BBS_TYPE_NEW = 2;
	public static final int BBS_TYPE_GOOD = 3;
	public static final int BBS_TYPE_ARTICLE = 5;
	private static final String TAG = "BbsFragment";
	private static final int PUBLISH_BBS_REQUEST_CODE = 0x123;
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private ArrayList<Fragment> fragmentsList;
	private ImageView ivBottomLine, search_img;
	private TextView tvTabAll, tvTabNew, tvTabGood, tvTabArticle;

	private int currIndex = 0;
	private int position_zero;
	private int position_one;
	private int position_two;
	private int position_three;

	private View rootView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		if (rootView == null) {
			rootView = inflater.inflate(R.layout.bbs_fragment_view, null);
			initView(rootView);
			initViewPage(rootView);
			initWidth(rootView);
		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}
		updateBottomLinePosition();
		return rootView;
	}

	private void initView(View view) {
		tvTabAll = (TextView) view.findViewById(R.id.tv_tab_all);
		tvTabNew = (TextView) view.findViewById(R.id.tv_tab_new);
		tvTabGood = (TextView) view.findViewById(R.id.tv_tab_good);
		tvTabArticle = (TextView) view.findViewById(R.id.tv_tab_article);

		tvTabAll.setOnClickListener(new MyOnClickListener(0));
		tvTabNew.setOnClickListener(new MyOnClickListener(1));
		tvTabGood.setOnClickListener(new MyOnClickListener(2));
		tvTabArticle.setOnClickListener(new MyOnClickListener(3));

		ImageView publish_bbs_view = (ImageView) view
				.findViewById(R.id.publish_bbs_image);
		publish_bbs_view.setOnClickListener(this);
	}

	@SuppressWarnings("deprecation")
	private void initViewPage(View view) {
		mViewPager = (ViewPager) view.findViewById(R.id.bbs_viewpager);
		if (fragmentsList != null && fragmentsList.size() != 0) {
			fragmentsList.clear();
		} else {
			fragmentsList = new ArrayList<Fragment>();
		}
		Fragment allfragment = new BbsAllFragment(BBS_TYPE_ALL);
		Fragment newFragment = new BbsNewFragment(BBS_TYPE_NEW);
		Fragment goodFragment = new BbsGoodFragment(BBS_TYPE_GOOD);
		Fragment articleFragment = new BbsArticleFragment(BBS_TYPE_ARTICLE);
		fragmentsList.add(allfragment);
		fragmentsList.add(newFragment);
		fragmentsList.add(goodFragment);
		fragmentsList.add(articleFragment);
		mAdapter = new CommonTabAdapter(getActivity().getSupportFragmentManager(),
				fragmentsList);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setCurrentItem(currIndex);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "onActivityCreated");
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	private void initWidth(View view) {
		ivBottomLine = (ImageView) view.findViewById(R.id.iv_bottom_line);
		search_img = (ImageView) view.findViewById(R.id.search_img);
		search_img.setOnClickListener(this);
		// int bottomLineWidth = ivBottomLine.getLayoutParams().width;
		int search_img_width = search_img.getLayoutParams().width;
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels - search_img_width * 2;
		int offset = (int) (screenW / 4.0);

		position_zero = search_img_width;
		position_one = offset + position_zero;
		position_two = 2 * offset + position_zero;
		position_three = 3 * offset + position_zero;
	}

	private void updateBottomLinePosition() {
		Animation animation = null;
		switch (currIndex) {
		case 0:
			animation = new TranslateAnimation(0, position_zero, 0, 0);
			tvTabAll.setTextColor(getActivity().getResources().getColor(
					R.color.bbs_tab_textview_color));
			break;
		case 1:
			animation = new TranslateAnimation(position_zero, position_one, 0,
					0);
			tvTabNew.setTextColor(getActivity().getResources().getColor(
					R.color.bbs_tab_textview_color));
			break;
		case 2:
			animation = new TranslateAnimation(position_one, position_two, 0, 0);
			tvTabGood.setTextColor(getActivity().getResources().getColor(
					R.color.bbs_tab_textview_color));
			break;
		case 3:
			animation = new TranslateAnimation(position_two, position_three, 0,
					0);
			tvTabArticle.setTextColor(getActivity().getResources().getColor(
					R.color.bbs_tab_textview_color));
			break;
		default:
			break;
		}

		animation.setFillAfter(true);
		animation.setDuration(300);
		ivBottomLine.startAnimation(animation);
	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mViewPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Log.d(TAG, "onPageSelected " + arg0);
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one,
							position_zero, 0, 0);
					tvTabNew.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, 0, 0, 0);
					tvTabGood.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three, 0, 0, 0);
					tvTabArticle.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				}
				tvTabAll.setTextColor(getActivity().getResources().getColor(
						R.color.bbs_tab_textview_color));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(position_zero,
							position_one, 0, 0);
					tvTabAll.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two,
							position_one, 0, 0);
					tvTabGood.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three,
							position_one, 0, 0);
					tvTabArticle.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				}
				tvTabNew.setTextColor(getActivity().getResources().getColor(
						R.color.bbs_tab_textview_color));
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_two, 0, 0);
					tvTabAll.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one,
							position_two, 0, 0);
					tvTabNew.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three,
							position_two, 0, 0);
					tvTabArticle.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				}
				tvTabGood.setTextColor(getActivity().getResources().getColor(
						R.color.bbs_tab_textview_color));
				break;
			case 3:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_three, 0, 0);
					tvTabAll.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one,
							position_three, 0, 0);
					tvTabNew.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two,
							position_three, 0, 0);
					tvTabGood.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_two,
							position_three, 0, 0);
					tvTabGood.setTextColor(getActivity().getResources()
							.getColor(R.color.bbs_tab_textview_hint_color));
				}
				tvTabArticle.setTextColor(getActivity().getResources()
						.getColor(R.color.bbs_tab_textview_color));
				break;
			}
			currIndex = arg0;
			if (animation != null) {
				animation.setFillAfter(true);
				animation.setDuration(300);
				ivBottomLine.startAnimation(animation);
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.publish_bbs_image: {
			Intent intent = new Intent(getActivity(), PublishBbsActivity.class);
			startActivityForResult(intent, PUBLISH_BBS_REQUEST_CODE);
		}

			break;
		case R.id.search_img: {
			Intent intent = new Intent(getActivity(), SearchTopicActivity.class);
			startActivity(intent);
		}
			break;
		default:
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case PUBLISH_BBS_REQUEST_CODE:

				break;

			default:
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
